--[[ Lambda Settings Center
     Part of LambdaUI made by Gaius
	 Graphics are made by Gaius
	 ------------------------------
	 Contact me in irc://phcnet.de/#wow-interface
--]]

LambdaObjectTable = {}
LambdaUI = {}
nilSettings = {}

LambdaLocale = {
 ["Health"] = {
  ["deDE"] = "Gesundheit",
  ["enUS"] = "health",
  ["frFR"] = "Santé",
 },
 ["Mana"] = {
  ["deDE"] = "Mana",
  ["enUS"] = "mana",
  ["frFR"] = "Mana",
 },
 ["Rage"] = {
  ["deDE"] = "Wut",
  ["enUS"] = "rage",
  ["frFR"] = "Rage",
 },
 ["Focus"] = {
  ["deDE"] = "Fokus",
  ["enUS"] = "focus",
  ["frFR"] = "Focus",
 },
 ["Energy"] = {
  ["deDE"] = "Energie",
  ["enUS"] = "energy",
  ["frFR"] = "Énergie",
 },
 ["Bag"] = {
  ["deDE"] = "Behälter",
  ["enUS"] = "Bag",
  ["frFR"] = "Sac",
 },
}

Lambda = CreateFrame("Frame")

function round2(num, idp)
  return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end

function Lambda:ColorChooser(element, newR, newG, newB, newA)
	if element == "Background" or element == "Overlay" or element == "Border" or element == "Panel" or element == "Orb" then
		for i,v in pairs(LambdaObjectTable) do
			if getglobal(v..element) then -- check if it has the Texture Instance
				getglobal(v..element):SetVertexColor(newR, newG, newB, newA)
			elseif string.find(v, "Raid") or string.find(v, "button") or string.find(v,"ActionBar") then
				getglobal(v):SetBackdropColor(newR, newG, newB, newA)
			end
		end
		
		if LambdaUI.ThemeSettings == nil then
			LambdaUI.ThemeSettings = {}
		end
		LambdaUI.ThemeSettings[element] = { newR, newG, newB, newA }
		
	end	
end 

function Lambda:Scale(element, newScale)
	if type(element) == "String" then
		for iterator,Object in pairs(LambdaObjectTable) do
			if Object == element then
				getglobal(Object):SetScale(newScale)
				
				if LambdaUI.ScaleSettings == nil then
					LambdaUI.ScaleSettings = {}
				end
				LambdaUI.ScaleSettings[Object] = newScale
			end
		end
	else
		element:SetScale(newScale)
		LambdaUI.ScaleSettings[element:GetName()] = newScale
	end
end

function Lambda:ListElements()
	ChatFrame1:AddMessage("verfügbare Elemente für SetScale, Unlock und Lock")
	for iterator, Object in pairs(LambdaObjectTable) do
		if string.find(Object,"(Lambda%w.-)") then
			ChatFrame1:AddMessage(Object)
		end
	end
end

function Lambda:Unlock(element)
	for iterator, Object in pairs(LambdaObjectTable) do
		if Object == element then
			getglobal(Object):RegisterForDrag("RightButton")
			getglobal(Object):SetMovable(true)
			getglobal(Object):EnableMouse(true)
			getglobal(Object):SetScript("OnDragStart", function()
											getglobal(Object):StartMoving()
										end)
			getglobal(Object):SetScript("OnDragStop", function()
											getglobal(Object):StopMovingOrSizing()
											if LambdaUI.PositionSettings == nil then
												LambdaUI.PositionSettings = {}
											end
											LambdaUI.PositionSettings[Object] = {getglobal(Object):GetPoint()}
										end)
		end
	end									
end

function Lambda:Lock(element)
	for iterator, Object in pairs(LambdaObjectTable) do
		if Object == element then
			getglobal(Object):SetScript("OnDragStart", function() end)
			getglobal(Object):SetScript("OnDragStop", function() end)
			getglobal(Object):SetMovable(false)
			getglobal(Object):RegisterForDrag("")
			getglobal(Object):EnableMouse(false)
		end
	end
end

Lambda:SetScript("OnEvent", function()
	if event == "VARIABLES_LOADED" then
		
		if LambdaUI.Settings == nilSettings or LambdaUI.Settings == nil then
			LambdaUI.Settings = {
				["HidePartyInRaid"] = true,
			}
		end
		if LambdaUI.VarTexts == nilSettings or LambdaUI.VarTexts == nil then
			LambdaUI.VarTexts = {
				["PlayerHealth"] = "UnitHealth(UnitId) ..' ('.. round2((UnitHealth(UnitId) / UnitHealthMax(UnitId)) * 100, 1) ..'% )'",
				["PlayerMana"] = "UnitMana(UnitId) ..' - '.. UnitManaMax(UnitId)",
			}
		end
		if LambdaUI.TextColors == nilSettings or LambdaUI.TextColors == nil then
			LambdaUI.TextColors = {
				["GlassFont"]  = { 0, 0, 0, 1 },
				["BorderFont"] = { 1, 1, 1, 1 },
				["OrbFont"]    = { 1, 1, 1, 1 },
			}
		end
		
		if LambdaUI.ThemeSettings == nilSettings or LambdaUI.ThemeSettings == nil then
			LambdaUI.ThemeSettings = {
				["Background"] 	= { 1  ,1  ,1,1, }, -- full opaque white
				["Border"] 		= { 0  ,0  ,0,1, }, -- full opaque black
				["Panel"] 		= { 0  ,0  ,0,1, }, -- full opaque black 
				["Orb"]			= { 0.2,0.6,1,1, }, -- Mana Color
				["Overlay"]     = { 1  ,1  ,1, 0.4}, -- semitransparent white
			}
		end
		
		for iterator, Object in pairs(LambdaObjectTable) do
			for element, Colors in pairs(LambdaUI.ThemeSettings) do
				if getglobal(Object..element) then
					getglobal(Object..element):SetVertexColor(unpack(Colors))
				elseif string.find(Object,"Raid%w.") then -- Raid pullout
						getglobal(Object):SetBackdropColor(unpack(Colors))
				end
			end
			if getglobal(Object) and getglobal(Object).UpdateFontColors then
				getglobal(Object):UpdateFontColors()
			end
			if getglobal(Object) then
				if LambdaUI.ScaleSettings ~= nil and LambdaUI.ScaleSettings[Object] then
					getglobal(Object):SetScale(LambdaUI.ScaleSettings[Object])
				else
					getglobal(Object):SetScale(1)
				end
				if LambdaUI.PositionSettings ~= nil and LambdaUI.PositionSettings[Object] then
					getglobal(Object):ClearAllPoints()
					getglobal(Object):SetPoint(unpack(LambdaUI.PositionSettings[Object]))
				end
			end
		end
	end
end)

local function RGBPercToHex(r, g, b, a)
	r = r <= 1 and r >= 0 and r or 0
	g = g <= 1 and g >= 0 and g or 0
	b = b <= 1 and b >= 0 and b or 0
	a = a <= 1 and a >= 0 and a or 0
	return string.format("%02x%02x%02x%02x",a*255, r*255, g*255, b*255)
end

function Lambda:SetTextColor(ColorName, newR, newG, newB, newA)
	if ColorName == "GlassFont" or ColorName == "BorderFont" or ColorName == "OrbFont" then
		LambdaUI.TextColors[ColorName] = {newR, newG, newB, newA};
	end
	for iterator,Object in pairs(LambdaObjectTable) do
		if getglobal(Object) and getglobal(Object).UpdateFontColors then
			getglobal(Object):UpdateFontColors()
		end
	end
end

Lambda:RegisterEvent("VARIABLES_LOADED")